import { getDvaApp } from 'umi';

export const isProductionEnv = (hasNodeEnv = true) => {
  if (hasNodeEnv) {
    return process.env.NODE_ENV == 'production' && CLIENT_ENV === 'release';
  }
  return CLIENT_ENV === 'release';
};

export const GetFileBase64 = (file) => {
  return new Promise((resolve, reject) => {
    const reader = new window.FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
};

/**
 * 广度优先遍历查找节点
 * @param {*} menuCode
 * @returns
 */
export const findNodeByCodeBFS = (menuCode) => {
  const permissions = getDvaApp()?._store?.getState()?.user?.buttonPermissions;
  const queue = [...permissions];

  while (queue.length > 0) {
    const node = queue.shift();
    if (node.menuCode === menuCode) {
      return node;
    }
    if (node.children && node.children.length > 0) {
      queue.push(...node.children);
    }
  }
  return null;
};
